package com.jeanbart.GUI;

import com.jeanbart.bean.Player;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;


public class FrameLR {
    public static final int EXIT_KEY_MARK = 0;
    static Vector rowData;
    static Vector columnNames;
    public static FrameLR instance;
    private FrameLR(){}
    //单例模式
    public static FrameLR getInstance(List<Player> blue, List<Player> red){
        if(instance == null){
            instance = new FrameLR();
            instance.runL(blue,"L");
            instance.runL(red,"R");
        }
        return instance;
    }
    public  void runL(List<Player> list, String LorR) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        final JFrame jf = new JFrame("kkk");
        jf.setSize(450,250);
        if(LorR.equals("L"))
            jf.setLocation(0,(int)screenSize.getHeight()/2-125);
        else
            jf.setLocation((int)screenSize.getWidth()-450,(int)screenSize.getHeight()/2-125);
        JPanel jPanel = new JPanel();
        jf.getContentPane().add(jPanel);

        columnNames = new Vector();
        columnNames.add("ID");
        columnNames.add("场次/胜率");
        columnNames.add("船");
        columnNames.add("场次/胜率");
        rowData = new Vector();
        list.sort(Comparator.comparing(Player::getRate).reversed());
        for (Player p:list) {
            Vector hang = new Vector();
            hang.add(p.getAccountName());
            if(p.getRate()>=0) {
                hang.add(p.getCount() + "/" + String.format("%.2f", p.getRate() * 100) + "%");
            }else{
                hang.add(p.getCount());
            }
            hang.add(p.getShipName());
            if(p.getShipRate()>=0) {
                hang.add(p.getShipCount() + "/" + String.format("%.2f", p.getShipRate() * 100) + "%");
            }else{
                hang.add(p.getShipCount());
            }
            rowData.add(hang);
        }
        DefaultTableModel Model = new DefaultTableModel(rowData,columnNames);

        JTable Table = new JTable(Model);
        Table.getTableHeader().setReorderingAllowed(false);
        Table=FitTableColumns(Table);
        setColumnColor(Table);
        Table.setEnabled(false);
        JScrollPane jsp = new JScrollPane(Table);
        GroupLayout gl_jPanel = new GroupLayout(jPanel);
        gl_jPanel.setHorizontalGroup(
                gl_jPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(gl_jPanel.createSequentialGroup()
                                .addGap(5)
                                .addGroup(gl_jPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(jsp, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        ));
        gl_jPanel.setVerticalGroup(
                gl_jPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(gl_jPanel.createSequentialGroup()
                        .addGroup(gl_jPanel.createSequentialGroup()
                                .addGap(5)
                                .addComponent(jsp, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        ));
        jPanel.setLayout(gl_jPanel);

        //第一步：注册热键，第一个参数表示该热键的标识，第二个参数表示组合键，如果没有则为0，第三个参数为定义的主要热键
        JIntellitype.getInstance().registerHotKey(EXIT_KEY_MARK, JIntellitype.MOD_ALT, (int)'5');

        //第二步：添加热键监听器
        JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {

            @Override
            public void onHotKey(int markCode) {
                switch (markCode) {
                    case EXIT_KEY_MARK:
                        instance = null;
                        jf.dispose();
                        break;
                }
            }
        });
        jf.setFocusable(false);
        jf.setAlwaysOnTop(true);
        jf.setUndecorated(true);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }


    /**
     * 使jtable自适应列宽度
     * @param myTable
     */
    public static JTable FitTableColumns(JTable myTable) {
        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();

        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
            int width = (int) myTable.getTableHeader().getDefaultRenderer()
                    .getTableCellRendererComponent(myTable, column.getIdentifier(), false, false, -1, col)
                    .getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable.getCellRenderer(row, col)
                        .getTableCellRendererComponent(myTable, myTable.getValueAt(row, col), false, false, row, col)
                        .getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column);
            column.setWidth(width + myTable.getIntercellSpacing().width + 10);
        }
        return myTable;
    }

    /**
     * 设置颜色间隔
     */
    public static void setColumnColor(JTable table) {
        try
        {
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer(){
                private static final long serialVersionUID = 1L;
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
                    if(row%2 == 0)
                        setBackground(Color.WHITE);//设置奇数行底色
                    else if(row%2 == 1)
                        setBackground(new Color(220,230,241));//设置偶数行底色
                    return super.getTableCellRendererComponent(table, value,isSelected, hasFocus, row, column);
                }
            };
            for(int i = 0; i < table.getColumnCount(); i++) {
                table.getColumn(table.getColumnName(i)).setCellRenderer(tcr);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


}
